package com.zlx.mall.biz.trolley.controller;

import com.zlx.mall.basic.annotations.ValidateToken;
import com.zlx.mall.basic.web.model.BaseRequest;
import com.zlx.mall.basic.web.model.BaseResponse;
import com.zlx.mall.biz.trolley.model.AddTrolleyRequest;
import com.zlx.mall.biz.trolley.service.ITrolleyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author xiatian
 * @Date 2025/3/23 星期日 18:17
 * @注释
 */
@RestController
public class TrolleyController {
    @Autowired
    private ITrolleyService trolleyService;

    @PostMapping("/api/trolley/add")
    @ValidateToken
    public BaseResponse addTrolley(BaseRequest request) {
        AddTrolleyRequest validate = request.toObjectAndValidate(AddTrolleyRequest.class);
        validate.setUserId(request.getUserId());
        trolleyService.addTrolley(validate);
        return BaseResponse.success();
    }

    @ValidateToken
    @PostMapping("/api/trolley/goodsList")
    public BaseResponse goodsList(BaseRequest request) {
        return BaseResponse.success(trolleyService.goodsList(request.getUserId()));
    }

    /**
     * 用户购物车总差价
     * @param request
     * @return
     */
    @ValidateToken
    @PostMapping("/api/trolley/diffPrice")
    public BaseResponse diffPrice(BaseRequest request) {
        List<Long> goodsIds = request.asArray("/goodsIds", Long.class);
        Long userId = request.getUserId();
        return BaseResponse.success(trolleyService.diffPrice(goodsIds,userId));
    }

    @ValidateToken
    @PostMapping("/api/trolley/select")
    public BaseResponse select(BaseRequest request) {
        return BaseResponse.success(trolleyService.select(request.asLong("/id"), request.asText("/select")));
    }
}
